<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Asset_employees_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function getData($id){
    	$this->db->where('employee_id',$id);
    	return $this->db->get('fam_employees')->result();
    }

    public function getLookUp($code)
    {
    	$this->db->where('enable_flag','Y');
    	$this->db->where('lookup_type',$code);
    	return  $this->db->get('app_lookup_values')->result();
    }

    public function insert_data($data){
    	$this->db->query('call fam_employees_ins(?,?,?,?,?,?,?,?,@id,@err,@msg)',$data);
    	$que = $this->db->query("SELECT @err as err, @id as id,@msg as msg");
		$msg;
		$err;
		$id;
		foreach ($que->result() as $keys) {
			$err = $keys->err;
			$msg = $keys->msg;
			$id  = $keys->id;
		}
		$arr = array(
			'err' => $err,
			'msg' => $msg,
			'id'  => $id
		);
		return $arr;
    }

    public function update_data($data){
        $this->db->query('call fam_employees_upd(?,?,?,?,@err,@msg)',$data);
        $que = $this->db->query("SELECT @err as err,@msg as msg");
        $msg;
        $err;
        foreach ($que->result() as $keys) {
            $err = $keys->err;
            $msg = $keys->msg;
        }
        $arr = array(
            'err' => $err,
            'msg' => $msg,
        );
        return $arr;
    }

    public function search($fname,$lname,$sex,$dep,$pos)
    {
    	$var = '';
    	$count = 0;
    	if(!empty($fname)){
    		$var .= ($count > 0 ? ' AND ' : '')."first_name LIKE '".$fname."' ";
    		$count++;
    	}

    	if(!empty($lname)){
    		$var .= ($count > 0 ? ' AND ' : '')."last_name LIKE '".$lname."' ";
    		$count++;
    	}

    	if(!empty($sex)){
    		$var .= ($count > 0 ? ' AND ' : '')."sex LIKE '".$sex."' ";
    		$count++;
    	}

    	if(!empty($dep)){
    		$var .= ($count > 0 ? ' AND ' : '')."department LIKE '".$dep."' ";
    		$count++;
    	}

    	if(!empty($pos)){
    		$var .= ($count > 0 ? ' AND ' : '')."position LIKE '".$pos."' ";
    		$count++;
    	}

    	return $this->db->query('SELECT * FROM fam_employees_v WHERE '.$var)->result();

    }
}